<?php
/**
 *    @package dotProject
 *    @subpackage modules
 *    @version $Revision: 1.0 $
*/

// include the powerful parent class that we want to extend for einstein
require_once( $AppUI->getSystemClass ('dp' ) );        // use the dPFramework for easy inclusion of this class here

/**
 * The Project_items Class
 */
class CMdProject_items extends CDpObject {
    // link variables to the einstein object (according to the existing columns in the database table einstein)
    var $pitem_item_id = NULL;
    var $pitem_BL_AW = NULL;
    var $pitem_invoice_no = NULL;
    var $pitem_currency = NULL;
    var $pitem_invoice_amount = NULL;
    var $pitem_PO_no = NULL;
    var $pitem_contract_no = NULL;
    var $pitem_quantity = NULL;
    var $pitem_quantity_unit = NULL;
    var $pitem_weight = NULL;
    var $pitem_weight_unit = NULL;
    var $pitem_volume = NULL;
    var $pitem_volume_unit = NULL;
    /*
ALTER TABLE `project_items` ADD `pitem_quantity_unit` INT( 11 ) NULL ,
ADD `pitem_weight_unit` INT( 11 ) NOT NULL ,
ADD `pitem_volume_unit` INT( 11 ) NOT NULL ;
ALTER TABLE `project_items` ADD `pitem_container_total` INT( 8 ) NULL ;
     */
    var $pitem_container = NULL;
    var $pitem_container_total = NULL;//
    var $pitem_package = NULL;
    var $pitem_stamp_date = NULL;
    var $pitem_customs_dec_no = NULL;
    var $pitem_custom_date = NULL;
    var $pitem_ETA_date = NULL;
    var $pitem_delivery_date = NULL;
    var $pitem_delivery_location = NULL;
    var $pitem_inspection = NULL;
    var $pitem_unloading = NULL;
    var $pitem_remarks = NULL;
    /*
     * ALTER TABLE `project_items` ADD `pitem_project` INT( 11 ) NOT NULL ;
     * ALTER TABLE `project_items` ADD `pitem_id` INT( 11 ) NOT NULL FIRST ;
     * ALTER TABLE `project_items` DROP PRIMARY KEY ,ADD PRIMARY KEY ( `pitem_id` );
     * ALTER TABLE `project_items` CHANGE `pitem_id` `pitem_id` INT( 11 ) NOT NULL AUTO_INCREMENT
     * ALTER TABLE `project_items` CHANGE `pitem_ETA_date` `pitem_ETA_date` DATETIME NULL DEFAULT NULL
     * ALTER TABLE `project_items` ADD `pitem_CO_no` varchar( 20 );
     * ALTER TABLE `project_items` ADD `pitem_stamp_time` varchar( 20 );
     */
    var $pitem_project = NULL;
    var $pitem_stamp_time = NULL;
    var $pitem_id = NULL;
    var $pitem_CO_no = NULL;
    function CMdProject_items() {
        $this->CDpObject( 'project_items', 'pitem_id' );
    }
//0918.124.282
//242699
    function delete() {
        $sql = "UPDATE project_items SET active = 0 WHERE pitem_item_id = $this->pitem_item_id";
        if (!db_exec( $sql )) {
            return db_error();
        } else {
            return NULL;
        }
    }
     function getPitemitemcodeName(){
        //$sql = "select item_name from items where item_code = '" . $this->pitem_item_code . "'";
        $q = new DBQuery;
        $q->addTable('items');
        $q->addQuery('item_name');
        $q->addWhere('item_id = '.$this->pitem_item_id);
        $sql = $q->prepare();
        $q->clear();
        $pitem_item_code = db_loadResult( $sql );
        return $pitem_item_code;
     }
     function getPitemcurrencyName(){
        //$sql = "select pitem_currency from md_currency where currency_id = '" . $this->pitem_currency . "'";
        $q = new DBQuery;
        $q->addTable('md_currency');
        $q->addQuery('currency_name');
        $q->addWhere('currency_id = '.$this->pitem_currency);
        $sql = $q->prepare();
        $q->clear();
        $pitem_currency = db_loadResult( $sql );
        return $pitem_currency;
     }
     function getPitemcontainerName(){
        //$sql = "select pitem_container from md_type where pitem_container = '" . $this->pitem_container . "'";
        $q = new DBQuery;
        $q->addTable('md_type');
        $q->addQuery('name');
        $q->addWhere('type = '.$this->pitem_container);
        $sql = $q->prepare();
        $q->clear();
        $pitem_container = db_loadResult( $sql );
        return $pitem_container;
     }

    function loadProjectItemsByProject($project_id) {
    	$q  = new DBQuery;

    	$q->dropTemp('wunit, qunit, vunit, container');
    	$q->exec();
		$q->clear();

		$q->createTemp('wunit');
		$q->addTable('md_unit');
		$q->addQuery('unit_id as wunit_id, unit_name as wunit_name');
		$q->exec();
		$q->clear();

		$q  = new DBQuery;
		$q->createTemp('qunit');
		$q->addTable('md_unit');
		$q->addQuery('unit_id as qunit_id, unit_name as qunit_name');
		$q->exec();
		$q->clear();

		// Volumn Unit
		$q  = new DBQuery;
		$q->createTemp('vunit');
		$q->addTable('md_unit');
		$q->addQuery('unit_id as vunit_id, unit_name as vunit_name');
		$q->exec();
		$q->clear();

		// Container type
		$q  = new DBQuery;
		$q->createTemp('container');
		$q->addTable('md_type');
		$q->addQuery('type_id as container_id, name as container_name');
		$q->exec();
		$q->clear();

		$q->addTable('project_items');
		$q->addWhere('pitem_project = ' . $project_id);
		$q->innerJoin('items', 'i', 'pitem_item_id = i.item_id');
		$q->leftJoin('wunit', 'wu', 'pitem_weight_unit = wu.wunit_id');
		$q->leftJoin('qunit', 'qu', 'pitem_quantity_unit = qu.qunit_id');
		$q->leftJoin('vunit', 'vu', 'pitem_volume_unit = vu.vunit_id');
		$q->leftJoin('container', 'con', 'pitem_container = con.container_id');
		$q->leftJoin('md_currency', 'currency', 'pitem_currency = currency.currency_id');
		$q->addQuery('project_items.*, i.item_name, i.item_code, wu.wunit_name as weight_name, qu.qunit_name as quantity_name, vu.vunit_name as volume_name, container_name, currency_abbreviation');
		$sql = $q->prepare();
		$q->clear();
		$k = db_loadList($sql);
		return $k;
    }
    
    function simpleLoadProjectItemsByProject($project_id) {
    	$q  = new DBQuery;

		$q->addTable('project_items');
		$q->addWhere('pitem_project = ' . $project_id);
		$q->addQuery('project_items.*');
		$k = $q->loadList();
		return $k;
    }
}
?>
